#! /usr/bin/env python
# -*- coding: utf-8 -*-


class Solution:
    """
    @param nums: The integer array.
    @param target: Target to find.
    @return: The first position of target. Position starts from 0.
    """
    # 找到第一次出现的下标
    def binarySearch(self, nums, target):
        start, end = 0, len(nums)       # [start, end)

        while start < end:
            mid = start + (end - start) // 2

            if nums[mid] == target:
                # 如果二分法找到了, 就反向查找第一次出现
                return self._find_first_idx(nums, mid, target)

            if target < nums[mid]:
                end = mid

            if target > nums[mid]:
                start = mid + 1

        return -1

    def _find_first_idx(self, nums, find_idx, target):
        while find_idx > 0:
            if nums[find_idx-1] == target:
                find_idx -= 1
            else:
                break

        return find_idx


def main():
    print("start main")

if __name__ == "__main__":
    main()
